package com.cyandpos.pad.pay.model;

import android.text.TextUtils;
import android.util.Log;


import com.cyandpos.pad.common.commondata.BaseResponse;
import com.cyandpos.pad.common.commondata.ErpCommonData;
import com.cyandpos.pad.common.netmanager.PosHttpManager;
import com.cyandpos.pad.http.OkHttpUtils;
import com.cyandpos.pad.posdata.CashCardItem;
import com.cyandpos.pad.posdata.ConsumeCrmPayData;
import com.cyandpos.pad.posdata.CzkCardItem;
import com.cyandpos.pad.posdata.GetValueCardResponse;
import com.cyandpos.pad.posdata.QueryMemberRequest;
import com.google.gson.JsonObject;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * Created by Administrator on 2017/5/9.
 */

public class PayCashCardModelImpl implements IPayCashCardModel {
    /**
     * 储值卡查询
     * @param type
     * @param conValue
     * @param password
     * @param verifiedCode
     * @param listener
     */
    @Override
    public void loadCashCardData(String type, String conValue, String password, String verifiedCode, final OnGetCashCardListener listener) {
        QueryMemberRequest requestParam = new QueryMemberRequest();
        requestParam.setCondType(Integer.parseInt(type));
        requestParam.setCondValue(conValue);
        requestParam.setPassword( password);
        requestParam.setVerifyCode( verifiedCode);
        PosHttpManager.QueryCashCard(requestParam, new OkHttpUtils.ResultCallback() {
            @Override
            public void onSuccess(JsonObject response) {
                BaseResponse<GetValueCardResponse> baseResponse = BaseResponse.fromJson(response.toString(),GetValueCardResponse.class);
                if (baseResponse.getCode().equals("0")) {
                    try {
                        GetValueCardResponse cashCard = baseResponse.getData();
                        listener.onGetCashCardSuccess(cashCard);
                    }catch (Exception e){
                        listener.onGetCashCardFailure(baseResponse.getCode() + baseResponse.getMsg() + e.getMessage().toString());
                    }

                } else {
                    if (TextUtils.isEmpty(baseResponse.getTips())){
                        listener.onGetCashCardFailure(baseResponse.getMsg());
                    }else {
                        listener.onGetCashCardFailure(baseResponse.getTips());
                    }
                }
            }
            @Override
            public void onFailure(String msg) {
                listener.onGetCashCardFailure(msg);
            }
        });
    }

    /**
     * 储值卡立减扣款核销接口
     * @param cardItemList
     * @param orderno
     * @param code
     * @param type
     * @param listener
     */
    @Override
    public void CashCardPay(final List<CashCardItem> cardItemList, String orderno, String code, int type, final OnPayCashCardListener listener) {
        ConsumeCrmPayData payData=new ConsumeCrmPayData();
        payData.setOrderno(orderno);
        payData.setCode(code);
        payData.setType(type);
        payData.setGuid(UUID.randomUUID().toString());
        payData.setCommit_crm(1);
        payData.setStoreCode(ErpCommonData.storeCode);
        ArrayList<CzkCardItem> list=new ArrayList<>();
        for (CashCardItem item:cardItemList){
//            if (!item.isHX_BJ()){
//                CzkCardItem cardItem=new CzkCardItem();
//                cardItem.setCardId(item.getCardId());
//                cardItem.setAmount(MoneyUtils.getMoneyStr(item.getmMoney()));
//                cardItem.setCardNo(item.getCardNo());
//                list.add(cardItem);
//            }
        }
        payData.setCzkCard(list);
        PosHttpManager.ConsumeCrmPay(payData, new OkHttpUtils.ResultCallback() {
            @Override
            public void onSuccess(JsonObject response) {
                Log.e("TAG","response=="+response.toString());
                String code = response.get("Code").getAsString();
                String msg = "";
                if (!response.get("Msg").isJsonNull()){
                    msg = response.get("Msg").getAsString();
                }
                if(code.equals("0")){
//                    for (CashCardItem item : cardItemList){
//                        item.setHX_BJ(true);
//                    }
                    listener.OnPayCashCardSuccess("成功");
                }else{
                    onFailure(msg);
//                    if (TextUtils.isEmpty(baseResponse.getTips())){
//
//                    }else {
//
//                    }
                }

            }

            @Override
            public void onFailure(String msg) {
                listener.OnPayCashCardFailure(msg);
            }
        });
    }


}
